import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import nprogress from 'nprogress';
import 'nprogress/nprogress.css';

import config from './config';

enum statusCode {
  RequestError = 400, //请求错误
  NotRole = 401, //未授权，请重新登录
  Reject = 403, //拒绝访问
  NotFind = 404, //请求出错
  ServeCrash = 405, //请求超时
  ServerError = 500, //服务器错误
  ServiceUnrealized = 501, //服务未实现
  NetworkError = 502, //网络错误
  ServiceUnavailable = 503, //服务不可用
  NetworkTimeout = 504, //网络超时
  HTTPNotSupported = 505, //HTTP版本不受支持
}

interface IResult<T> {
  code: number;
  message: string;
  data: T;
}

class Request {
  instance: AxiosInstance;

  constructor(config: AxiosRequestConfig) {
    this.instance = axios.create(config);

    this.instance.interceptors.request.use(
      (res: AxiosRequestConfig) => {
        //从仓库中获取token，在非passport页面请求加入
        /*  if (passportStore.utmToken) {
               res.headers!.Authorization = passportStore.utmToken;
             } */
        nprogress.start();
        return res;
      },
      (err: any) => Promise.reject(err),
    );

    this.instance.interceptors.response.use(
      (res: AxiosRequestConfig) => {
        nprogress.done();
        return res.data;
      },
      (err: any) => {
        nprogress.done();
        switch (err.response.status) {
          case statusCode.RequestError:
            console.log('请求错误');
            break;
          case statusCode.NotRole:
            console.log('未授权，请重新登录');
            break;
          case statusCode.Reject:
            console.log('拒绝访问');
            break;
          case statusCode.NotFind:
            console.log('请求出错');
            break;
          case statusCode.ServeCrash:
            console.log('请求超时');
            break;
          case statusCode.ServerError:
            console.log('服务器错误');
            break;
          case statusCode.ServiceUnrealized:
            console.log('服务未实现');
            break;
          case statusCode.NetworkError:
            console.log('网络错误');
            break;
          case statusCode.ServiceUnavailable:
            console.log('服务不可用');
            break;
          case statusCode.NetworkTimeout:
            console.log('网络超时');
            break;
          case statusCode.HTTPNotSupported:
            console.log('HTTP版本不受支持');
            break;
          default:
            console.log(`连接出错(${err.response.status})!`);
        }
        return Promise.reject(err);
      },
    );
  }

  public request(config: AxiosRequestConfig): Promise<AxiosResponse> {
    return this.instance.request(config);
  }

  public get<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<IResult<T>>> {
    return this.instance.get(url, config);
  }

  public post<T>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig,
  ): Promise<AxiosResponse<IResult<T>>> {
    return this.instance.post(url, data, config);
  }

  public put<T>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig,
  ): Promise<AxiosResponse<IResult<T>>> {
    return this.instance.put(url, data, config);
  }

  public delete<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<IResult<T>>> {
    return this.instance.delete(url, config);
  }
}

export default new Request(config);
